import java.util.Random;
import java.util.Stack;

public class MySort {

    public static void main(String[] args) {
        int[] arr = new int[5];
        Random random = new Random();
        for (int j = arr.length-1;j>=0;j--){
            arr[arr.length-j-1] = j;
        }
        long a = System.currentTimeMillis();

        //quickSort(arr,0, arr.length-1);
        //quickSort2(arr);
        //merge(arr,0, arr.length-1);
        //merge2(arr);
        sort1(arr);

        //soo(arr);
        long b = System.currentTimeMillis();
        //quickSort(arr,0, arr.length-1);
        for (int i = 0; i < arr.length ; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println(b-a);


    }
    //直接插入排序，希尔排序，冒泡排序，选择排序，快速排序（递归or栈），堆排序，归并排序（递归or栈）；




    //直接插入排序
    /*时间复杂度：最好的时候有序：O(n);最坏:O(n^2);
    * 空间复杂度:0(1);没有多申请数组空间；
    * 稳定性：稳定的（一个本身就不稳定的排序是不可能变成稳定的，但一个本身稳定的程序是可以变成稳定的）
    * */

    public static void sort1(int arr[]) {
        for (int i = 1; i < arr.length ; i++) {
            int j = i - 1;
            int tem = arr[i];
            for(;j >= 0; j--) {
                //这里加不加等于号，决定了其稳定性
                if(arr[j] > tem) {
                   swap(arr,j,j+1);
                }
                else{

                    break;
                }
                //arr[j] = tem;

            }

        }
    }

    //希尔排序（缩小增量排序）
    /*时间复杂度：待定就是，n^1.2到n^1.6；空间复杂度：O(1)
    稳定性：不稳定；
    * */
    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap = gap/3 + 1;
            shell(arr,gap);
        }
    }
    public static void shell(int[] arr,int gap){
        for (int i = gap ; i < arr.length; i++) {
            int j = i - gap;
            int tmp = arr[i];
            for(;j >= 0 ; j = j - gap) {
                if(arr[j] > tmp ) {
                    arr[j + gap] = arr[j];
                }
                else {
                    break;
                }
                arr[j] = tmp;
            }
        }
    }
    //选择排序
    /* 时间复杂度：O(n^2);
       空间复杂度:O(1);
       不稳定;
    * */
    public static void checkSort(int[] arr){
        for (int i = 0 ; i < arr.length - 1;i++ ) {
            int j = i+1;
            int index = i;
            for (;j < arr.length ; j++) {
                if(arr[j] < arr[index]) {
                    index = j;
                }
            }
            swap(arr,i,index);
        }
    }

    public static  void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    public static void soo(int[] arr){
        for(int i = 0; i < arr.length ; i++){
            for(int j = 0; j < arr.length - i -1;j++) {
                if(arr[j] > arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
    }
    public static  void checkSort3(int[] arr) {
        int left = 0;
        int right = arr.length - 1;

        while(left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1 ; i < right ; i++) {
                if(arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
                if (arr[i] > arr[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(arr , left , minIndex );
            if(maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(arr , right, maxIndex);
            left++;
            right--;
        }
        if(arr[left] < arr[right]) {
            swap(arr , right , left);
        }

    }
    //快速排序hoare法

    public  static void quickSort(int[] arr,int left , int right){
        if (left >= right){
            return;
        }
        //快速排序优化二，当小于某个区间的时候，数据趋于有序，那么我们可以用直接插入排序
        if(right - left + 1<= 10) {
            sort2(arr, left, right);
            return;
        }
        //快速排序优化第一步：三角取中间key;
        int result = findMiddle(arr,left,right);
        swap(arr,left,result);

        int middle = quick(arr,left,right);
        quickSort(arr,left , middle - 1);
        quickSort(arr,middle + 1, right);




    }
    //快速排序非递归法
    public  static void quickSort2(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        int result = findMiddle(arr,left,right);
        swap(arr,left,result);
        int middle = quick(arr,left,right);
        Stack<Integer> stack = new Stack<>();
       if(middle + 1 < right) {
           stack.push(right);
           stack.push(middle + 1);

       }
       if(middle - 1 >left) {
           stack.push(middle - 1);
           stack.push(left);
       }
       while(!stack.empty()) {
           left = stack.pop();
           right = stack.pop();
           middle = quick(arr , left ,right);
           if(middle + 1 < right) {
               stack.push(right);
               stack.push(middle + 1);
           }
           if(middle - 1 >left) {
               stack.push(middle - 1);
               stack.push(left);
           }

       }


    }
    public static void sort2(int arr[],int left ,int right) {
        for (int i = left + 1; i < right + 1 ; i++) {
            int j = i - 1;
            int tem = arr[i];
            for(;j >= left; j--) {
                //这里加不加等于号，决定了其稳定性
                if(arr[j] > tem) {
                    arr[j+1] = arr[j];
                }
                else{
                    break;
                }
                arr[j ] = tem;
            }
        }
    }
    public static int findMiddle(int[] arr, int left , int right) {
        int middle = (left + right) / 2;
        if (arr[left] > arr[right]) {
            if (arr[left] < arr[middle]) {
                return left;
            } else if (arr[right] > arr[middle]) {
                return right;
            } else {
                return middle;
            }
        }
        if (arr[right] > arr[left]) {
            if (arr[right] < arr[middle]) {
                return right;
            } else if (arr[left] > arr[middle]) {
                return left;
            } else {
                return middle;
            }

        }
        return left;
    }
   /* public  static  int quick(int[] arr, int start , int end){
        int left = start;
        int right = end;
        while(left < right) {
            while(left < right && arr[right] >= arr[start]){
                right--;
            }
            while(left < right && arr[left] <= arr[start]){
                left++;
            }

            swap(arr,left,right);
        }
        swap(arr,left,start);
        return  left;
    }*/
    //挖坑法
    public static int quick(int[] arr, int left , int right){
        int tmp = arr[left];

        while(left < right) {
            while(left < right && arr[right] >= tmp){
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] <= tmp){
                left++;
            }
            arr[right] = arr[left];


        }
        arr[left] = tmp;
        return left;

    }
    //归并排序
    //时间复杂度：nlog2N;
    //空间复杂度：O(n)
    //稳定性:稳定
    public  static void merge(int[] arr, int left ,int right){
        if(left >= right) {
            return;
        }
        int middle = left + ((right - left)>>1);
        merge(arr,left,middle);
        merge(arr,middle + 1,right);
        combine(arr,left,right,middle);


    }

    //归并排序非递归
    public static void merge2(int[] arr) {
        int gap = 1;

        while(gap < arr.length) {

            for(int i = 0 ; i < arr.length;i = i + 2 * gap){
               int  left = i ;

                int middle = left + gap - 1;
                if(middle >= arr.length){
                    middle = arr.length - 1;
                }

                int right = gap + middle;
                if(right >= arr.length){
                    right = arr.length - 1;
                }


                combine(arr,left,right,middle);
            }
            gap *= 2;
        }
    }
    public static void combine(int[] arr,int left , int right , int middle){
        int s1 = left;
        int e1 = middle;
        int s2 = middle + 1;
        int e2 = right;
        int[] arr1 = new int[right - left + 1];
        int k = 0;
        while(s1 <= e1 && s2 <= e2) {
            if(arr[s1] >= arr[s2]) {
                arr1[k++] = arr[s2++];
            }else {
                arr1[k++] = arr[s1++];
            }
        }
        if(s1 > e1){
            for(;s2 <= e2; s2++) {
                arr1[k++] = arr[s2];
            }
        }else {
            for (; s1 <= e1; s1++) {
                    arr1[k++] = arr[s1];
                }
        }
        for(int i = 0;i < k;i++){
            arr[i + left] = arr1[i];
        }
    }
}